package org.rugby.online.impl;

import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

import org.rugby.online.core.exception.RboException;
import org.rugby.online.core.humans.RboHuman;
import org.rugby.online.core.humans.RboNonPlayer;
import org.rugby.online.core.humans.RboProfession;
import org.rugby.online.core.log.RboLogger;
import org.rugby.online.core.skills.RboSkill;
import org.rugby.online.core.skills.RboSkillType;
import org.rugby.online.core.utils.NameUtils;

public class DefaultNonPlayer extends DefaultHuman implements RboNonPlayer, RboHuman {

	/**
	 * Serial
	 */
	private static final long serialVersionUID = 2265699759949965483L;
	
	/**
	 * Minimal age
	 */
	private static final int MINIMAL_AGE = 18;
	
	/**
	 * Maximal age
	 */
	private static final int MAXIMAL_AGE = 68;

	private RboProfession profession;
	private Map<String, RboSkill> skillMap;
	
	/**
	 * Constructor
	 * @param profession the profession
	 * @param locale the locale for the name
	 */
	public DefaultNonPlayer(RboProfession profession, Locale locale) {
		super();
		try {
			this.setFirstName(NameUtils.getRandomFirstName(locale));
			this.setName(NameUtils.getRandomLastName(locale));
			this.initSkillMap(profession);
			this.setAge((short) (new Random().nextInt(MAXIMAL_AGE - MINIMAL_AGE) + MINIMAL_AGE));
			this.profession = profession;
		} catch (RboException e) {
			RboLogger.parse(e);
		}
	}
	
	/**
	 * Initialize the map of skills according to the profession
	 * @param profession the profession 
	 */
	private void initSkillMap(RboProfession profession) {
		skillMap = new ConcurrentHashMap<String, RboSkill>(0);
		for (RboSkillType type : RboSkillType.values()) {
			if (type.toString().startsWith(profession.toString())) {
				skillMap.put(type.toString(), new DefaultSkill(type));
			}
		}
	}

	@Override
	public RboProfession getProfession() {
		return profession;
	}

	@Override
	public Map<String, RboSkill> getSkillMap() {
		return skillMap;
	}

}
